//
//  CSBClubXQSweeping.swift
//  CurSodoboAer
//
//  Created by CurSodoboAer on 2024/11/5.
//

import UIKit
import JDStatusBarNotification

class CSBClubXQSweeping: UIViewController {
    private let csbBackComButton = UIButton.init()
    private let csbAlertComButton = UIButton.init()
    private let bottometCoverView = UIView.init()
    private let uderNamerCSB = UILabel()
    
    
    private let csbBottonJoinComButton = UIButton.init()
    var clubDataCSB:Dictionary<String,String>
    init(qugaoshCSB:[Int]?,activeDataCSB: Dictionary<String, String>) {
        
        if qugaoshCSB?.first ?? 0 > 0 {
            self.clubDataCSB = activeDataCSB
        }else{
            self.clubDataCSB = activeDataCSB
        }
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        bottometCoverView.layer.cornerRadius = 30
        bottometCoverView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        uderNamerCSB.textColor = .white
        uderNamerCSB.textAlignment = .left
        uderNamerCSB.font = UIFont(name: "Helvetica Bold", size:22)
        
        uderNamerCSB.text = clubDataCSB["csbClubName"] ?? ""
        
        csbBottonJoinComButton.setImage(UIImage.init(named: "csbCall_Click"), for: .normal)
        
        
        view.backgroundColor = UIColor(red: 0.64, green: 0.41, blue: 0.98, alpha: 1)
       
        csbBackComButton.setImage(UIImage.init(named: "csb_back"), for: .normal)
        csbBackComButton.addTarget(self, action: #selector(csbButtonClick), for: .touchUpInside)
        csbAlertComButton.setImage(UIImage.init(named: "active_report"), for: .normal)
        csbAlertComButton.addTarget(self, action: #selector(csbrepotrtClick), for: .touchUpInside)
        
        csbBottonJoinComButton.addTarget(self, action: #selector(csbrjCallClick), for: .touchUpInside)
       
        
        
        
        addAotherCSB()
        
        addCenterINfoVire()
    }
    
    private func addAotherCSB()  {
        let topCovrer = UIImageView(image: UIImage.init(named: clubDataCSB["csbClubCover"] ?? ""))
        topCovrer.contentMode = .scaleAspectFill
        view.addSubview(topCovrer)
        view.addSubview(csbBackComButton)
        view.addSubview(csbAlertComButton)
        topCovrer.addSubview(uderNamerCSB)
        view.addSubview(bottometCoverView)
       
        view.addSubview(csbBottonJoinComButton)
        
       
        topCovrer.snp.makeConstraints { make in
            make.leading.trailing.top.equalToSuperview()
            make.height.equalTo(280.xQQ)
        }
        let stattusBarHeight = UIApplication.shared.statusBarFrame.height
        csbBackComButton.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(12.xQQ)
            make.width.equalTo(24)
            make.height.equalTo(22)
            make.top.equalToSuperview().offset(stattusBarHeight + 15)
        }
        
        csbAlertComButton.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.trailing.equalToSuperview().inset(12.xQQ)
            make.centerY.equalTo(csbBackComButton)
        }
        
        uderNamerCSB.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(14.xQQ)
            make.bottom.equalToSuperview()
        }
        
        
        bottometCoverView.backgroundColor = UIColor(red: 0.64, green: 0.41, blue: 0.98, alpha: 1)
        bottometCoverView.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.top.equalTo(uderNamerCSB.snp.bottom).offset(16.xQQ)
        }
       
        
        csbBottonJoinComButton.snp.makeConstraints { make in
            make.width.equalTo(60.xQQ)
            make.height.equalTo(60.xQQ)
            make.right.equalToSuperview().offset(-12)
            make.bottom.equalToSuperview().offset(-64)
        }
        
        
    }
    
    
    private func addCenterINfoVire()  {
        
        let fengScore = clubDataCSB["csbClubStart"] ?? ""
        
        for kk in 0...4 {
            let hedCSB = UIImageView(image: UIImage.init(named: "manxing_csb"))
            hedCSB.layer.cornerRadius = 18.xQQ
            hedCSB.layer.masksToBounds = true
            
            hedCSB.contentMode = .scaleAspectFill
            bottometCoverView.addSubview(hedCSB)
            hedCSB.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset( 12 + 44.xQQ*CGFloat(kk) )
                make.top.equalToSuperview().offset(20.xQQ)
                make.width.height.equalTo(32.xQQ)
            }
            
            if fengScore == "4"  && kk == 4{
                hedCSB.image = UIImage.init(named: "kongxing_csb")
            }else if fengScore == "4.5"  && kk == 4{
                hedCSB.image = UIImage.init(named: "banxing_csb")
            }
        }
        
        let scorelvbl = UILabel.init()
        scorelvbl.textColor = .white
        scorelvbl.textAlignment = .left
        scorelvbl.font = UIFont(name: "Futura Medium", size: 17)
        scorelvbl.text = fengScore
       
        let titlrlvbl = UILabel.init()
        titlrlvbl.textColor = .white
        titlrlvbl.textAlignment = .left
        titlrlvbl.font = UIFont(name: "Futura Medium", size: 22)
        titlrlvbl.text = "Introduction"
        
        
        let alldestailrlvbl = UITextView.init()
        alldestailrlvbl.textColor = .white
        alldestailrlvbl.textAlignment = .left
        alldestailrlvbl.font = UIFont(name: "Futura Medium", size: 15)
        alldestailrlvbl.text = clubDataCSB["csbClubPref"] ?? ""
        alldestailrlvbl.isEditable = false
        alldestailrlvbl.backgroundColor = .clear
        
        

        
      
        
        bottometCoverView.addSubview(scorelvbl)
        bottometCoverView.addSubview(titlrlvbl)
        bottometCoverView.addSubview(alldestailrlvbl)
        
      
        scorelvbl.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(240.xQQ)
            make.top.equalToSuperview().offset(32.xQQ)
        }
     
        
        titlrlvbl.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(14.xQQ)
            make.top.equalToSuperview().offset(72)
        }
        
        
        alldestailrlvbl.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(14.xQQ)
            make.top.equalTo(titlrlvbl.snp.bottom).offset(12)
            make.bottom.equalToSuperview()
        }
    }
    
    @objc func csbButtonClick()  {
        self.navigationController?.popViewController(animated: true)
    }

    @objc func csbrepotrtClick()  {
        self.navigationController?.pushViewController(CSBAIrAlertSweeping.init(), animated: true)
    }
    
    @objc func csbrjCallClick()  {
        if let textcallnum = clubDataCSB["csbClubTel"] {
            guard let phoneNumber = URL(string:"tel://" + textcallnum ) else {
                      
                NotificationPresenter.shared.present("Invalid phone number", includedStyle: .warning)
                       return
                   }
                   
                   // 检查是否可以拨打电话
                   if UIApplication.shared.canOpenURL(phoneNumber) {
                       // 打开电话应用并拨打电话
                       UIApplication.shared.open(phoneNumber, options: [:], completionHandler: nil)
                   } else {
                       NotificationPresenter.shared.present("Phone calls are not supported on this device", includedStyle: .warning)
                      
                   }
        }
    }
    
  
    
}
